<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\home\controller;

use controller\BasicAdmin;
use service\WechatService;
use think\App;
use think\Db;
use think\facade\Cache;

/**
 *
 *
 * 前端基类
 * @author caicai
 */
class HomeBasic extends BasicAdmin
{
    public $user_id = '';
    /**
     * 判断是否登录
     */
    public function _initialize()
    {
        if (!isset($_COOKIE['JSESSIONID']) || !$userInfo = Cache::get('userInfo')){
            $this->user_id = $userInfo['id'];
            return json_encode(['userInfo' => ['code' => '101']]);
        } else {
            $userInfo = Cache::get('userInfo');
            if ($_COOKIE['JSESSIONID']!==$userInfo['session3rd']) {
                return json_encode(['userInfo' => ['code' => '101']]);
            } else {
                //成功
            }
        }
    }


    /**
     *
     * 小程序消息推送
     * @param $touser 用户OPENID
     * @param $template_id 消息模板
     * @param $form_id form ID
     * @param $data 消息内容
     * @return array
     */

    public function templateSend($touser , $form_id, $template_id , $data ){
        $config = [
            'mini_appid' => sysconf('mini_appid'),
            'mini_appsecret' => sysconf('mini_appsecret'),
        ];
        //获取授权通行证
        $access_token =  WechatService::WeMiniBasic()->getMiniAccessToken($config);
        $arr = [
            'touser' => $touser,
            'template_id' => $template_id,
            'form_id' => $form_id,
            'data' => $data,
        ];
        //推送消息
        $res =  WechatService::WeMiniBasic()->templateSend($access_token,json_encode($arr));
        return $res; // errcode==0 成功
    }

}
